//
//  ExperienceController.m
//  SmallEasyLife
//
//  Created by qianfeng on 15/10/23.
//  Copyright © 2015年 GXK. All rights reserved.
//

#import "ExperienceController.h"
#import "EXDataManager.h"
#import "EXHeadCell.h"
#import "EXListCell.h"

#import "EXHeadController.h"
#import "EXEventController.h"

@interface ExperienceController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSArray * exListDataArray;
@property (nonatomic, strong) NSArray * exHeadDataArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ExperienceController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadDataFromCache];
}
- (void)loadDataFromCache
{
//    if (self.exDataArray == nil) {
//        self.exDataArray = [[NSMutableArray alloc] init];
//    }
//    if (self.exHeadDataArray == nil) {
//        self.exHeadDataArray = [[NSArray alloc] init];
//    }
    
    
    NSString * path1 = [[NSBundle mainBundle] pathForResource:@"ExperiencePage" ofType:@"json"];
    NSData * data1 = [NSData dataWithContentsOfFile:path1];
    
    NSDictionary * dic1 = [NSJSONSerialization JSONObjectWithData:data1 options:NSJSONReadingMutableContainers error:nil];
    
    NSArray * modelArrList = dic1[@"list"];
    NSArray * modelArrHead = dic1[@"head"];
    
    //head数组里面用的是eventModel
    self.exHeadDataArray = [EXDataManager analysisEXHead:modelArrHead];
    self.exListDataArray = [EXDataManager analysisEXHead:modelArrList];
    
//    NSLog(@"exHeadDataArray -> %@",self.exHeadDataArray);
//    NSLog(@"exListDataArray -> %@",self.exListDataArray);

    [self.tableView reloadData];
    
}

#pragma mark - tableView delegate;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.exListDataArray.count + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 150;
    }
    
    return 300;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellID = @"EXListCell";
    
    if (indexPath.row == 0) {
        cellID = @"EXHeadCell";
        EXHeadCell * headCell = [tableView dequeueReusableCellWithIdentifier:cellID];
        headCell.modelArray = self.exHeadDataArray;
        headCell.ownerController = self;
        return headCell;
    }
    EXListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if(indexPath.row - 1 >= 0)
    {
        cell.model = self.exListDataArray[indexPath.row - 1];
        cell.ownerController = self;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    //EXEventSegue
    //EXHeadSegue
    if ([segue.identifier isEqualToString:@"EXHeadSegue"]) {
        if ([segue.destinationViewController isKindOfClass:[EXHeadController class]]) {
            EXHeadController * controller = segue.destinationViewController;
            
            controller.model = sender;
//            NSLog(@"controller.adUrl -> %@",controller.adUrl);
        }
    }
    if ([segue.identifier isEqualToString:@"EXEventSegue"]) {
        if ([segue.destinationViewController isKindOfClass:[EXEventController class]]) {
            EXEventController * controller = segue.destinationViewController;
            
            controller.model = sender;
        }
    }
    
    NSLog(@"%@, yuan -> %@",segue,segue.sourceViewController);
}


@end
